/**
 * 
 */
package es.cloudcast.persistence.jdo.action.basic;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import es.cloudcast.persistence.PersistenceException;
import es.cloudcast.persistence.dao.jdo.PersistenceManagerFactory;
import es.cloudcast.persistence.jdo.action.Action;

/**
 * Executes SQL query in the datastore
 * 
 * @author Samuel García Martínez <in0de>
 *
 */
public class ExecuteSelectSQLAction implements Action {

	protected Class<?> clazz;
	protected String query;
	
	public ExecuteSelectSQLAction(Class<?> clazz, String query) {
		this.clazz = clazz;
		this.query = query;
	}
	
	/* (non-Javadoc)
	 * @see es.cloudcast.persistence.jdo.action.Action#process(es.cloudcast.persistence.jdo.PersistenceManagerFactory)
	 */
	public Object process(PersistenceManagerFactory PMF)
			throws PersistenceException {
		List<?> result = null;
		
		try {
			PersistenceManager pManager = 
				PMF.getCurrentPersistenceManager();
			Query query = pManager.newQuery(Query.SQL, this.query);		
			
			result =  (List<?>) query.execute();
		} catch(Exception ex) {
			throw new PersistenceException(ex);
		}
		
		return result;
	}

}
